/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaFormItem } from "@mineadmin/form";
//@ts-ignore
import RemoteSelect from "@/components/ma-remote-select/index.vue";
import { CourseVo, page as CoursePage } from "@/modules/course/api/manage";
import {
  CourseSectionVo,
  page as SectionPage,
} from "@/modules/course/api/section";

export default function getFormItems(
  formType: "add" | "edit" = "add",
  t: any,
  model: CourseSectionVo
): MaFormItem[] {
  if (formType === "add") {
    // model.password = "123456";
  }

  return [
    {
      label: () => t("course-manage.title"),
      prop: "course_id",
      cols: { md: 12 },
      render: () => RemoteSelect,
      renderProps: {
        multiple: false,
        class: "w-full",
        placeholder: t("form.pleaseSelect", {
          msg: t("course-manage.title"),
        }),
        api: () =>
          new Promise((resolve) => resolve(CoursePage({ page_size: 999 }))),
        dataHandle: (response: any) => {
          return response.data.list?.map((item: CourseVo) => {
            return { label: `${item.title}`, value: item.id };
          });
        },
      },
    },
    {
      label: () => t("course-section.parent"),
      prop: "parent_id",
      cols: { md: 12 },
      render: () => RemoteSelect,
      renderProps: {
        multiple: false,
        class: "w-full",
        placeholder: t("form.pleaseSelect", {
          msg: t("course-section.parent"),
        }),
        api: () =>
          new Promise((resolve) => resolve(SectionPage({ page_size: 999 }))),
        dataHandle: (response: any) => {
          const list = response.data.list?.map((item: CourseSectionVo) => {
            return { label: `${item.name}`, value: item.id };
          });
          list.unshift({ label: "无", value: 0 });
          return list.filter(l=>l.value!=model.id);
        },
      },
    },
    {
      label: () => t("course-section.name"),
      prop: "name",
      render: "input",
      renderProps: {
        placeholder: t("form.pleaseInput", { msg: t("course-section.name") }),
      },
      itemProps: {
        rules: [
          {
            required: true,
            message: t("form.requiredInput", { msg: t("course-section.name") }),
          },
        ],
      },
    },
  ];
}
